package com.csun.cmny.provider.controller;


import com.csun.cmny.base.dto.LoginAuthDto;
import com.csun.cmny.core.annotation.LogAnnotation;
import com.csun.cmny.core.annotation.ValidateAnnotation;
import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.dto.OmcLifeActionOldmanDto;
import com.csun.cmny.provider.model.constant.LongRangeServiceOrderStatusConstant;
import com.csun.cmny.provider.model.constant.LongRangeServiceOrderTypeConstant;
import com.csun.cmny.provider.model.domain.*;
import com.csun.cmny.provider.model.dto.longRangeServiceOrder.*;
import com.csun.cmny.provider.model.dto.service.ServiceOrderOperationDto;
import com.csun.cmny.provider.model.vo.longRangeServiceOrder.LongRangeServiceOrderVo;
import com.csun.cmny.provider.pojo.Oldman;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.*;
import com.csun.cmny.provider.utils.JPushService;
import com.csun.cmny.util.PublicUtil;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import com.github.pagehelper.PageInfo;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping(value = "/longRangeServiceOrder", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class OmcLongRangeServiceOrderController extends BaseController {

    @Resource
    private CmnyMessageSource cmnyMessageSource;

    @Resource
    private OmcLongRangeServiceOrderService longRangeServiceOrderService;

    @Resource
    private OmcSpecialistService specialistService;

    @Resource
    private OmcServiceSecondLevelService  serviceSecondLevelService;

    @Resource
    private OmcMerchandiseService merchandiseService;

    @Resource
    private OmcVideoLongRangeServiceService videoLongRangeServiceService;

    @Resource
    private OmcSuperviseLongRangeServiceService superviseLongRangeServiceService;


    /*订单分页查询*/
    @ValidateAnnotation
    @PostMapping(value = "/select")
    public Wrapper select(@RequestBody @Valid LongRangeServiceOrderSelectDto longRangeServiceOrderSelectDto, BindingResult br){

        List<LongRangeServiceOrderVo>longRangeServiceOrderVos=longRangeServiceOrderService.selectLongRangeServiceOrder(longRangeServiceOrderSelectDto);

        PageInfo<LongRangeServiceOrderVo> pageInfo = new PageInfo<>(longRangeServiceOrderVos);


        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null),pageInfo);
    }



    /*远程视频订单下单*/
    @ValidateAnnotation
    @PostMapping(value = "/videoPlaceAnOrder")
    public Wrapper videoPlaceAnOrder(@RequestBody @Valid LongRangeServiceOrderDto longRangeServiceOrderDto, BindingResult br){

        //下单数量是否正常
       /* if (longRangeServiceOrderDto.getNumbers()<1){
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("longRangeServiceOrder.number.error", null));
        }*/

        //专家是否存在
        OmcSpecialist omcSpecialist = specialistService.selectByKey(longRangeServiceOrderDto.getSpecialistId());
        if (PublicUtil.isEmpty(omcSpecialist)){
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("longRangeServiceOrder.specialist.null", null));
        }

        //老人是否存在
       /* Oldman oldman = new Oldman();
        oldman.setOldmanId(longRangeServiceOrderDto.getOlamanId().intValue());
        Oldman select = oldmanService.select(oldman);
        if (PublicUtil.isEmpty(select)){
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("longRangeServiceOrder.oldman.null", null));
        }*/

        Long result=longRangeServiceOrderService.videoPlaceAnOrder(longRangeServiceOrderDto,getLoginAuthDto());

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null),result);
    }

    /*咨询订单下单*/
    @ValidateAnnotation
    @PostMapping(value = "/consultPlaceAnOrder")
    public Wrapper consultPlaceAnOrder(@RequestBody @Valid LongRangeServiceOrderDto longRangeServiceOrderDto, BindingResult br){

        //专家是否存在
        OmcSpecialist omcSpecialist = specialistService.selectByKey(longRangeServiceOrderDto.getSpecialistId());
        if (PublicUtil.isEmpty(omcSpecialist)){
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("longRangeServiceOrder.specialist.null", null));
        }

        //老人是否存在
       /* Oldman oldman = new Oldman();
        oldman.setOldmanId(longRangeServiceOrderDto.getOlamanId().intValue());
        Oldman select = oldmanService.select(oldman);
        if (PublicUtil.isEmpty(select)){
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("longRangeServiceOrder.oldman.null", null));
        }*/

        Long result=longRangeServiceOrderService.consultPlaceAnOrder(longRangeServiceOrderDto,getLoginAuthDto());

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null),result);
    }

    /*专家确认远程视频订单*/
    @PostMapping(value = "/videoEnter")
    public Wrapper videoEnter(Long id){

        OmcLongRangeServiceOrder omcLongRangeServiceOrder = longRangeServiceOrderService.selectByKey(id);
        //订单不存在
        if (PublicUtil.isEmpty(omcLongRangeServiceOrder)){
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("longRangeServiceOrder.isNull", null));
        }
        //订单不是远程视频订单
        if (omcLongRangeServiceOrder.getOrderType()!= LongRangeServiceOrderTypeConstant.REMOTESUPERVISION){
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("consult.longRangeServiceOrder.type.video.error", null));
        }
        //订单不是待确认状态
        if (omcLongRangeServiceOrder.getOrderStatus()!=LongRangeServiceOrderStatusConstant.TOBECONFIRMED){
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("longRangeServiceOrder.enert.error", null));
        }

        OmcSpecialist omcSpecialist = specialistService.selectByKey(omcLongRangeServiceOrder.getSpecialistId());

        //更改订单状态
        omcLongRangeServiceOrder.setResidueNumber(omcSpecialist.getNumberMonthly());
        omcLongRangeServiceOrder.setEnretTime(new Date());
        omcLongRangeServiceOrder.setOrderStatus(LongRangeServiceOrderStatusConstant.ALREADYIDENTIFIED);
        int result = longRangeServiceOrderService.update(omcLongRangeServiceOrder);


        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null),result);
    }



    /*发起远程通话*/
    @PostMapping(value = "/video")
    public Wrapper video(Long id){

        OmcLongRangeServiceOrder omcLongRangeServiceOrder = longRangeServiceOrderService.selectByKey(id);
        //订单不存在
        if (PublicUtil.isEmpty(omcLongRangeServiceOrder)){
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("longRangeServiceOrder.isNull", null));
        }
        //订单不是远程视频订单
        if (omcLongRangeServiceOrder.getOrderType()!= LongRangeServiceOrderTypeConstant.REMOTESUPERVISION){
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("consult.longRangeServiceOrder.type.video.error", null));
        }
        //订单次数已经用完
        if (omcLongRangeServiceOrder.getOrderStatus()==LongRangeServiceOrderStatusConstant.NUMBEROFCOMPLETE){
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("consult.longRangeServiceOrder.type.video.number.null", null));
        }
        //已经过期
        if (omcLongRangeServiceOrder.getOrderStatus()==LongRangeServiceOrderStatusConstant.ALREADYEXPIRED){
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("consult.longRangeServiceOrder.type.video.time.null", null));
        }
        //订单专家还没有确认
        if (omcLongRangeServiceOrder.getOrderStatus()==LongRangeServiceOrderStatusConstant.TOBECONFIRMED){
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("consult.longRangeServiceOrder.type.video.not.tobeconfirmed", null));
        }

        int result=longRangeServiceOrderService.video(omcLongRangeServiceOrder,getLoginAuthDto());

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null),result);
    }

    /*接通远程通话*/
    @PostMapping(value = "/connect")
    public Wrapper connect (Long videoId){

        OmcVideoLongRangeService omcVideoLongRangeService = videoLongRangeServiceService.selectByKey(videoId);

        OmcLongRangeServiceOrder omcLongRangeServiceOrder = longRangeServiceOrderService.selectByKey(omcVideoLongRangeService.getOrderId());
        //订单不存在
        if (PublicUtil.isEmpty(omcLongRangeServiceOrder)){
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("longRangeServiceOrder.isNull", null));
        }
        //订单不是远程视频订单
        if (omcLongRangeServiceOrder.getOrderType()!= LongRangeServiceOrderTypeConstant.REMOTESUPERVISION){
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("consult.longRangeServiceOrder.type.video.error", null));
        }
        //订单次数已经用完
        if (omcLongRangeServiceOrder.getOrderStatus()==LongRangeServiceOrderStatusConstant.NUMBEROFCOMPLETE){
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("consult.longRangeServiceOrder.type.video.number.null", null));
        }
        //已经过期
        if (omcLongRangeServiceOrder.getOrderStatus()==LongRangeServiceOrderStatusConstant.ALREADYEXPIRED){
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("consult.longRangeServiceOrder.type.video.time.null", null));
        }

        int result=longRangeServiceOrderService.connect(omcVideoLongRangeService,omcLongRangeServiceOrder,getLoginAuthDto());

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null),result);
    }

    /*结束远程通话*/
    @PostMapping(value = "/finish")
    public Wrapper finish (Long videoId){

        OmcVideoLongRangeService videoLongRangeService = videoLongRangeServiceService.selectByKey(videoId);

        OmcLongRangeServiceOrder omcLongRangeServiceOrder = longRangeServiceOrderService.selectByKey(videoLongRangeService.getOrderId());
        //订单不存在
        if (PublicUtil.isEmpty(omcLongRangeServiceOrder)){
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("longRangeServiceOrder.isNull", null));
        }
        //订单不是远程视频订单
        if (omcLongRangeServiceOrder.getOrderType()!= LongRangeServiceOrderTypeConstant.REMOTESUPERVISION){
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("consult.longRangeServiceOrder.type.video.error", null));
        }
        //订单次数已经用完
        /*if (omcLongRangeServiceOrder.getResidueNumber()<1){
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("consult.longRangeServiceOrder.type.video.number.null", null));
        }*/
        //已经过期
        if (omcLongRangeServiceOrder.getOrderStatus()==LongRangeServiceOrderStatusConstant.ALREADYEXPIRED){
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("consult.longRangeServiceOrder.type.video.time.null", null));
        }

        int result=longRangeServiceOrderService.finish(videoLongRangeService,omcLongRangeServiceOrder,getLoginAuthDto());

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null),result);
    }

    /*远程监测订单下单*/
    @ValidateAnnotation
    @PostMapping(value = "/superviseAnOrder")
    public Wrapper superviseAnOrder(@RequestBody @Valid LongRangeServiceOrderSuperviseDto longRangeServiceOrderSuperviseDto, BindingResult br){


        Long result=longRangeServiceOrderService.superviseAnOrder(longRangeServiceOrderSuperviseDto,getLoginAuthDto());

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null),result);
    }

    /*远程监测订单机构确认*/
    @ValidateAnnotation
    @PostMapping(value = "/superviseEnter")
    public Wrapper superviseEnter(@RequestBody @Valid LongRangeServiceOrderSuperviseEnterDto longRangeServiceOrderSuperviseEnterDto, BindingResult br){

        OmcLongRangeServiceOrder omcLongRangeServiceOrder = longRangeServiceOrderService.selectByKey(longRangeServiceOrderSuperviseEnterDto.getOrderId());

        OmcSpecialist specialist = specialistService.selectByKey(longRangeServiceOrderSuperviseEnterDto.getSpecialistId());


        //订单不存在
        if (PublicUtil.isEmpty(omcLongRangeServiceOrder)){
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("longRangeServiceOrder.isNull", null));
        }
        //订单不是远程监测订单
        if (omcLongRangeServiceOrder.getOrderType()!=LongRangeServiceOrderTypeConstant.SUPERVISE){
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("consult.longRangeServiceOrder.type.supervise.error", null));
        }
        //订单不是待确认状态
        if (omcLongRangeServiceOrder.getOrderStatus()!=LongRangeServiceOrderStatusConstant.TOBECONFIRMED){
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("longRangeServiceOrder.enert.error", null));
        }
        //不是本机构的订单
        if (!omcLongRangeServiceOrder.getGroupId().equals(getLoginAuthDto().getGroupId())){
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("longRangeServiceOrder.enert.error.not.group", null));
        }
        //专家不存在
        if (PublicUtil.isEmpty(specialist)){
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("consult.specialist.isNull", null));
        }
        //不是本机构专家
        if (!specialist.getGroupId().equals(getLoginAuthDto().getGroupId())){
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("longRangeServiceOrder.specialist.not.my.geoup", null));
        }

        int result=longRangeServiceOrderService.superviseEnter(longRangeServiceOrderSuperviseEnterDto,getLoginAuthDto());

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null),result);
    }

    /*远程监测订单上门监测*/
    @ValidateAnnotation
    @PostMapping(value = "/theDoorMonitoring")
    public Wrapper theDoorMonitoring(@RequestBody @Valid ServiceOrderOperationDto serviceOrderOperationDto, BindingResult br){

        OmcLongRangeServiceOrder omcLongRangeServiceOrder = longRangeServiceOrderService.selectByKey(serviceOrderOperationDto.getId());

        //订单不存在
        if (PublicUtil.isEmpty(omcLongRangeServiceOrder)){
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("longRangeServiceOrder.isNull", null));
        }
        //订单不是远程监测订单
        if (omcLongRangeServiceOrder.getOrderType()!=LongRangeServiceOrderTypeConstant.SUPERVISE){
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("consult.longRangeServiceOrder.type.supervise.error", null));
        }
        //订单已经过期
        if (omcLongRangeServiceOrder.getOrderType()==LongRangeServiceOrderStatusConstant.ALREADYEXPIRED){
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("consult.longRangeServiceOrder.type.video.time.null", null));
        }
        //订单次数已经用完
        if (omcLongRangeServiceOrder.getResidueNumber()<1){
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("consult.longRangeServiceOrder.type.video.number.null", null));
        }

        //不是本机构的订单
        if (!omcLongRangeServiceOrder.getGroupId().equals(getLoginAuthDto().getGroupId())){
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("longRangeServiceOrder.enert.error.not.group", null));
        }

        int result=longRangeServiceOrderService.theDoorMonitoring(serviceOrderOperationDto,omcLongRangeServiceOrder,getLoginAuthDto());

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null),result);
    }

    /*远程监测订单上门监测离开*/
    @ValidateAnnotation
    @PostMapping(value = "/doorMonitoringLeave")
    public Wrapper doorMonitoringLeave(@RequestBody @Valid ServiceOrderOperationLeaveDto serviceOrderOperationLeaveDto, BindingResult br){

        OmcSuperviseLongRangeService omcSuperviseLongRangeService = superviseLongRangeServiceService.selectByKey(serviceOrderOperationLeaveDto.getSuperviseId());

        OmcLongRangeServiceOrder omcLongRangeServiceOrder = longRangeServiceOrderService.selectByKey(omcSuperviseLongRangeService.getOrderId());

        //订单不存在
        if (PublicUtil.isEmpty(omcLongRangeServiceOrder)){
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("longRangeServiceOrder.isNull", null));
        }
        //订单不是远程监测订单
        if (omcLongRangeServiceOrder.getOrderType()!=LongRangeServiceOrderTypeConstant.SUPERVISE){
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("consult.longRangeServiceOrder.type.supervise.error", null));
        }

        //不是本机构的订单
        if (!omcLongRangeServiceOrder.getGroupId().equals(getLoginAuthDto().getGroupId())){
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("longRangeServiceOrder.enert.error.not.group", null));
        }

        int result=longRangeServiceOrderService.doorMonitoringLeave(omcSuperviseLongRangeService,serviceOrderOperationLeaveDto,omcLongRangeServiceOrder,getLoginAuthDto());

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null),result);
    }


    /*新增远程监测类商品*/
    @ValidateAnnotation
    @PostMapping(value = "/addMerchandise")
    public Wrapper addMerchandise(@RequestBody @Valid LongRangeSuperviseAnOrderMerchandiseDto longRangeSuperviseAnOrderMerchandiseDto, BindingResult br){

        OmcServiceSecondLevel omcServiceSecondLevel = serviceSecondLevelService.selectByKey(longRangeSuperviseAnOrderMerchandiseDto.getSecondLevellId());

        if (PublicUtil.isEmpty(omcServiceSecondLevel)){
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("merchandise.secondLevellId.is.null", null));
        }

        OmcMerchandise merchandise = new OmcMerchandise();
        merchandise.setMerchandiseName(longRangeSuperviseAnOrderMerchandiseDto.getMerchandiseName());
        merchandise.setGroupId(getLoginAuthDto().getGroupId());
        merchandise.setSecondLevellId(longRangeSuperviseAnOrderMerchandiseDto.getSecondLevellId());
        List<OmcMerchandise> select = merchandiseService.select(merchandise);
        if (PublicUtil.isNotEmpty(select)){
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("merchandise.merchandiseName.error", null));
        }

        int result=merchandiseService.addSupervise(longRangeSuperviseAnOrderMerchandiseDto,getLoginAuthDto());


        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null),result);
    }

    /*
     * 订单支付
     * */
    @ValidateAnnotation
    @PostMapping(value = "/pay/{id}")
    public Wrapper pay (@PathVariable Long id) {
        int result=longRangeServiceOrderService.pay(id);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null),result);
    }
}
